package com.deyang.mycommon.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.deyang.mycommon.R;
import com.deyang.mycommon.util.DensityUtil;


public class TopBar extends RelativeLayout {
    private boolean leftImgIsShow;//左侧图标是否显示
    private Drawable leftImg;
    private int leftImgWidth;
    private int leftImgHeight;

    private boolean leftTextIsShow;//右侧文字是否显示
    private String leftText;//左边文字属性
    private float leftTextSize;
    private int leftTextColor;
    private Drawable leftTextBg;


    private String centerText;//中间文字属性
    private float centerTextSize;
    private int centerTextColor;

    private boolean rightTextIsShow;//右侧文字是否显示
    private String rightText;//右边文字属性
    private float rightTextSize;
    private int rightTextColor;
    private Drawable rightTextBg;

    private boolean rightImgIsShow;//右侧图标是否显示
    private int rightImgWidth;
    private int rightImgHeight;
    private Drawable rightImg;

    private LinearLayout leftLinearLayout;
    //左右图标
    private ImageView imgRight;
    //左中右文字
    private TextView txtLeft, txtCenter, txtRight;
    //图片文字布局
    private LayoutParams leftImgParams, leftTxtParams, centerParams, rightTxtParams, rightImgParams;

    private LeftImgClick leftImgClick;
    private LeftClick leftClick;
    private CenterClick centerClick;
    private RightImgClick rightImgClick;
    private RightClick rightClick;

    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @SuppressLint("NewApi")
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获得attrs.xml中自定的所有属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        //左图默认不显示
        leftImgIsShow = array.getBoolean(R.styleable.TopBar_leftImgIsShow, true);
        if (leftImgIsShow) {
            leftImg = array.getDrawable(R.styleable.TopBar_leftImg);
            leftImgWidth = array.getDimensionPixelSize(R.styleable.TopBar_leftImgWidth, 0);
            leftImgHeight = array.getDimensionPixelSize(R.styleable.TopBar_leftImgHeight, 0);
            leftLinearLayout = new LinearLayout(context);
            leftLinearLayout.setOrientation(LinearLayout.VERTICAL);
            leftLinearLayout.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(leftImgWidth + DensityUtil.dip2px(context, 20),
                    leftImgHeight + DensityUtil.dip2px(context, 20));
            params.gravity = Gravity.CENTER;
            leftLinearLayout.setLayoutParams(params);

            ImageView imgLeft = new ImageView(context);
            imgLeft.setBackground(leftImg);
            LinearLayout.LayoutParams leftImgParams = new LinearLayout.LayoutParams(leftImgWidth == 0 ? LayoutParams.WRAP_CONTENT : leftImgWidth,
                    leftImgHeight == 0 ? LayoutParams.WRAP_CONTENT : leftImgHeight);
            imgLeft.setLayoutParams(leftImgParams);
            leftLinearLayout.addView(imgLeft);

            addView(leftLinearLayout);
        }

        //根据属性ID左边文字属性值
        leftTextIsShow = array.getBoolean(R.styleable.TopBar_leftText, true);
        if (leftTextIsShow) {
            leftText = array.getString(R.styleable.TopBar_leftText);
            leftTextSize = array.getFloat(R.styleable.TopBar_leftTextSize, 16);
            leftTextColor = array.getColor(R.styleable.TopBar_leftTextColor, Color.WHITE);
            leftTextBg = array.getDrawable(R.styleable.TopBar_leftTextBg);
        }

        //根据属性ID中间文字属性值
        centerText = array.getString(R.styleable.TopBar_centerText);
        centerTextSize = array.getFloat(R.styleable.TopBar_centerTextSize, 18);
        centerTextColor = array.getColor(R.styleable.TopBar_centerTextColor, Color.WHITE);

        //根据属性ID右边文字属性值
        rightTextIsShow = array.getBoolean(R.styleable.TopBar_rightTextIsShow, true);
        if (rightTextIsShow) {
            rightText = array.getString(R.styleable.TopBar_rightText);
            rightTextColor = array.getColor(R.styleable.TopBar_rightTextColor, Color.WHITE);
            rightTextSize = array.getFloat(R.styleable.TopBar_rightTextSize, 16);
            rightTextBg = array.getDrawable(R.styleable.TopBar_rightTextBg);
        }

        //右图默认不显示
        rightImgIsShow = array.getBoolean(R.styleable.TopBar_rightImgIsShow, false);
        if (rightImgIsShow) {
            rightImg = array.getDrawable(R.styleable.TopBar_rightImg);
            rightImgWidth = array.getDimensionPixelSize(R.styleable.TopBar_rightImgWidth, 0);
            rightImgHeight = array.getDimensionPixelSize(R.styleable.TopBar_rightImgHeight, 0);
            imgRight = new ImageView(context);
            imgRight.setBackground(rightImg);
            rightImgParams = new LayoutParams(rightImgWidth == 0 ? LayoutParams.WRAP_CONTENT : DensityUtil.dip2px(context, rightImgWidth),
                    rightImgHeight == 0 ? LayoutParams.WRAP_CONTENT : DensityUtil.dip2px(context, rightImgHeight));
            rightImgParams.setMargins(DensityUtil.dip2px(context, 10), 0, DensityUtil.dip2px(context, 10), 0);
            rightImgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            rightImgParams.addRule(RelativeLayout.CENTER_VERTICAL);
            addView(imgRight, rightImgParams);
        }
        //属性获取完之后及时回收
        array.recycle();

        txtLeft = new TextView(context);
        txtLeft.setText(leftText);
        txtLeft.setTextSize(TypedValue.COMPLEX_UNIT_SP, leftTextSize);
        txtLeft.setTextColor(leftTextColor);
        txtLeft.setBackground(leftTextBg);
        //左边文本设置位置
        leftTxtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftTxtParams.setMargins(DensityUtil.dip2px(context, 10), 0, DensityUtil.dip2px(context, 10), 0);
        if (null == leftImgParams) {
            //addRule一个参数
            leftTxtParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        } else {
            //addRule两个参数，第二个参数是相对控件的ID
            leftTxtParams.addRule(RelativeLayout.RIGHT_OF, leftLinearLayout.getId());
            leftTxtParams.addRule(RelativeLayout.END_OF, leftLinearLayout.getId());
        }
        leftTxtParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(txtLeft, leftTxtParams);

        txtCenter = new TextView(context);
        txtCenter.setText(centerText);
        txtCenter.setTextSize(TypedValue.COMPLEX_UNIT_SP, centerTextSize);
        txtCenter.setTextColor(centerTextColor);
        //中间文本定位
        centerParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        centerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(txtCenter, centerParams);

        txtRight = new TextView(context);
        txtRight.setText(rightText);
        txtRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, rightTextSize);
        txtRight.setTextColor(rightTextColor);
        txtRight.setPadding(DensityUtil.dip2px(context, 10), 0, DensityUtil.dip2px(context, 10), 0);
        txtRight.setBackground(rightTextBg);
        //右边文本定位
        rightTxtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (null == rightImgParams) {
            //addRule一个参数
            rightTxtParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        } else {
            rightTxtParams.setMargins(DensityUtil.dip2px(context, 10), 0, DensityUtil.dip2px(context, 10), 0);
            rightTxtParams.addRule(RelativeLayout.LEFT_OF, imgRight.getId());
            rightTxtParams.addRule(RelativeLayout.START_OF, imgRight.getId());
        }
        rightTxtParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(txtRight, rightTxtParams);

        //左边文字点击事件
        txtLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                leftClick(leftClick);
            }
        });

        //中间文字点击事件
        txtCenter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                centerClick(centerClick);
            }
        });

        if (null != imgRight) {
            //右图点击事件
            imgRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    rightImgClick(rightImgClick);
                }
            });
        }

        if (null != leftLinearLayout) {
            //左图点击事件
            leftLinearLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    leftImgClick(leftImgClick);
                }
            });
        }

        //右侧文字点击事件
        txtRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rightClick(rightClick);
            }
        });
    }


    public void setLeftImgClickListener(LeftImgClick leftImgClick) {
        this.leftImgClick = leftImgClick;
    }

    public void setLeftClickListener(LeftClick leftClick) {
        this.leftClick = leftClick;
    }

    public void setCenterClickListener(CenterClick centerClick) {
        this.centerClick = centerClick;
    }

    public void setRightImgClickListener(RightImgClick rightImgClick) {
        this.rightImgClick = rightImgClick;
    }

    public void setRightClickListener(RightClick rightClick) {
        this.rightClick = rightClick;
    }

    //左侧图片点击，根据实际需求修改
    public void leftImgClick(LeftImgClick leftImgClick) {
        if (null != leftImgClick) {
            leftImgClick.leftImgClickListener();
        } else {
            ((Activity) getContext()).finish();
        }
    }

    //左侧点击，根据实际需求修改
    public void leftClick(LeftClick leftClick) {
        if (null != leftClick) {
            leftClick.leftClickListener();
        } else {
            ((Activity) getContext()).finish();
        }
    }

    //中间点击，根据实际需求修改
    public void centerClick(CenterClick centerClick) {
        if (null != centerClick) {
            centerClick.centerClickListener();
        }
    }

    //右侧图片点击，根据实际需求修改
    public void rightImgClick(RightImgClick rightImgClick) {
        if (null != rightImgClick) {
            rightImgClick.rghtImgClickListener();
        }
    }

    //右侧点击，根据实际需求修改
    public void rightClick(RightClick rightClick) {
        if (null != rightClick) {
            rightClick.rghtClickListener();
        }
    }

    //对外暴露可以设置标题的方法
    public void setCenterText(String centerText) {
        if (null != txtCenter) {
            txtCenter.setText(centerText);
        }
    }

    //对外暴露可以设置右边文字显示的方法
    public void setRightTextIsShow(boolean isShow) {
        if (isShow) {
            txtRight.setVisibility(VISIBLE);
        } else {
            txtRight.setVisibility(GONE);
        }
    }

    public void setRightImgIsShow(boolean isShow) {
        if (this.rightImgIsShow) {
            if (isShow) {
                imgRight.setVisibility(VISIBLE);
            } else {
                imgRight.setVisibility(GONE);
            }
        }
    }

    public interface LeftImgClick {
        void leftImgClickListener();
    }

    public interface LeftClick {
        void leftClickListener();
    }

    public interface CenterClick {
        void centerClickListener();
    }

    public interface RightImgClick {
        void rghtImgClickListener();
    }

    public interface RightClick {
        void rghtClickListener();
    }

}
